package models;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.IdClass;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Column;
import javax.persistence.MapsId;
import javax.persistence.PrimaryKeyJoinColumn;

import play.db.jpa.Model;

@Entity
@Table(name="ServiceHasParticipants")
public class ServiceHasParticipants
{
	@EmbeddedId
	public ServiceAssociationId id;
	
	public enum Status {
		WAITING, APPROVED, REJECTED
    }
	
	@Column
	public Status status = Status.WAITING;
  
	@ManyToOne
	@MapsId("serviceId")
	public Service service;
	
	@ManyToOne
	@MapsId("userId")
	public User user;
	
	public ServiceHasParticipants(Service service, User user) {
		this(service, user, Status.WAITING);
	}
	
	public ServiceHasParticipants(Service service, User user, Status status) {
		this.setRelationsAndId(service, user);
		this.status = status;
	}
	
	private void setRelationsAndId(Service service, User user) {
		this.service = service;
		this.user = user;
		
		// create the id of the instance.
		this.id = new ServiceAssociationId();
		this.id.serviceId = service.id;
		this.id.userId = user.id;
	}
	
	public boolean equals(Object o) {
		ServiceHasParticipants so = (ServiceHasParticipants) o;
		
		return this.service.id == so.service.id && this.user.id == so.user.id;
	}
	
	public int hashCode() {
		return (int) (this.service.id + this.user.id);
	}
}